/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package functions;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Database;

/**
 *
 * @author JArik
 */
public class Healings {

    public static int addHealing(int dogId, String name, String res, Date fVisit, Date lVisit) throws SQLException
    {
        Database db = new Database();

        int result=0;
        if(dogId <= 0)
            throw new SQLException("Неправельное животное");
        if(name.isEmpty())
            throw new SQLException("Имя курса не может быть пустым");
        if(fVisit == null)
            throw new SQLException("Не указана дата курса/визита");
        fVisit = new Date(fVisit.getTime());
        if(lVisit != null)
        lVisit = new Date(lVisit.getTime());
        ResultSet set = db.insertDBInfo("healing", "dogId, name, result,firstVisit, lastVisit",
                 dogId+",'"+name+"','"+res+"','"+fVisit+"','"+lVisit+"'");
        if(set.next())
            result = set.getInt(1);
        db.closeStatement();
        return result;
    }

    public static void deteteHealing(int healId) throws SQLException
    {
        Database db = new Database();
        db.deleteDBInfo("healing", "id="+healId);
        
        db.closeStatement();
        db = null;
    }
    public static ArrayList<HealingSet> getVisitsByDog(int id) throws SQLException
    {
        Database db = new Database();
        ResultSet set = db.getDBInfo("id,dogId,name,result,firstVisit,lastVisit", "healing", "dogId="+id);
        ArrayList<HealingSet> res = new ArrayList<HealingSet>();
        while(set.next())
        {
            HealingSet hSet = new HealingSet(set.getInt(1));
            hSet.setName(set.getString("name"));
            hSet.setResult(set.getString("result"));
            hSet.setFirstVisit(set.getDate("firstVisit"));
            hSet.setLastVisit(set.getDate("lastVisit"));
            res.add(hSet);
        }
        db.closeStatement();
        return res;
    }

    public static void updateHealing(int healId, HealingSet set) throws SQLException {
        Database db = new Database();
        String query="";
        if(healId <= 0)
            throw new SQLException("Неправельный курс");
        if(set.getName().isEmpty())
            throw new SQLException("Неправельное название курса");
        if(set.getLastVisit() == null)
            throw new SQLException("Неуказана дата лечения");
        query += "name = '"+set.getName()+"' ";
        query += ", lastVisit='"+set.getLastSQLVisit()+"' ";
        query += ", name='"+set.getName()+"' ";
        if(set.getResult() != null)
        {
            query +=", result='"+set.getResult()+"' ";
        }
        db.updateDBInfo("healing", query, "id="+healId);
        db.closeStatement();
    }


}

